<?php
#基础函数
include_once('_path.php');
include_once("../includes/init.php");

#获取页面变量
$action = trim($_GET['action']);
$id= trim($_REQUEST['id']) + 0;
	$quote['id'] = string_sql($id);
#如果有group_id
if ($id > 0) {
	$sql = "SELECT * FROM region WHERE id=$id";
	$_region = $database->queryFirst($sql);
	if (!$_region['id']) {
		$_REQUEST['referrer'] = "index.php";
		redirect("记录不存在!");
	}
}
#得到post的值
$name = trim($_REQUEST['name']); 
	$quote['name'] = string_sql($name);
$province = trim($_REQUEST['province']);
$city = trim($_REQUEST['city']);
	$quote['province'] = string_sql($province.' '.$city);
#如果是编辑
if ($action == "edit.form"  || $action == "members.form" || $action == "members.view") {
	$name = $_region['name'];
	$price = $_region['province'];
}
#如果是添加操作
if ($action == "add.do") {
	verify_field("name", "网点名称", "empty");

	if (!count($error_strs)) {
		$sql = "INSERT INTO region (name, province) VALUES (".$quote['name'].", ".$quote['province'].");";
		$database->execute($sql);		
		$_REQUEST['referrer'] = "index.php";
		redirect("添加网点成功!");
	} else {
		$action = "add.form";
	}
#如果是编辑操作
} else if ($action == "edit.do" && $id > 0) {

	verify_field("name", "网点名称", "empty");

	if (!count($error_strs)) {
		$sql = "UPDATE region SET name = ".$quote['name'].", province = ".$quote['province']." WHERE id = ".$quote['id'].";";
		$rs = $database->execute($sql);

		$_REQUEST['referrer'] = "index.php";
		redirect("修改网点成功!");
	} else {
		$action = "edit.form";
	}
#如果是删除操作
} else if ($action == "delete.do" && $id > 0) {
	
	$database->execute("DELETE FROM region WHERE id = ".$quote['id'].";");
        $_REQUEST['referrer'] = "index.php";
        redirect("删除网点成功!");
}else if($action == 'members.form'){
        #翻页
    $page = trim($_GET['page']) + 0; if ($page == "") { $page = $_COOKIE['csmpage']; }
    $pagesize = 20;
    #!!翻页
    if ($page < 1) $page = 1;

    #总共数量
    $rs_count = $database->queryArray("SELECT count(*) total FROM members_info WHERE region=".string_sql($name));
    $total_page = ceil($rs_count[0]["total"] / $pagesize);
    if ($total_page < 1) $total_page = 1;
    if ($page > $total_page) $page = $total_page;
    set_cookie('csmpage', $page, time() + 604800);
}else if($action == 'members.view'){
        #翻页
    $page = trim($_GET['page']) + 0; if ($page == "") { $page = $_COOKIE['csmpage']; }
    $pagesize = 20;
    #!!翻页
    if ($page < 1) $page = 1;

    #总共数量
    $rs_count = $database->queryArray("SELECT count(*) total FROM customers WHERE region=".string_sql($name));
    $total_page = ceil($rs_count[0]["total"] / $pagesize);
    if ($total_page < 1) $total_page = 1;
    if ($page > $total_page) $page = $total_page;
    set_cookie('csmpage', $page, time() + 604800);
}
#样式
$css = array();
$js = array("jquery-1.4.2.min","area","areat");
#菜单
$menu = "site";
include_once("../layout/_header.php");
?>
<div class="main">
<div class="traininc-nav-list">
<ul>
<li><a href="index.php">网点列表</a></li>
<?php
if($action == "add.form"){
?>
<li class="selected"><a href="javascript:;">新增网点</a></li>
<?php
}elseif($action == "edit.form"){
?>
<li class="selected"><a href="javascript:;">修改网点</a></li>
<?php
}elseif($action=="members.form"){
?>
<li class="selected"><a href="javascript:;">会员列表</a></li>
<?php
}elseif($action=="members.view"){
?>
<li class="selected"><a href="javascript:;">企业列表</a></li>
<?php
}
?>
</ul>
</div>
<?
if ($action == "add.form") {
?>
<form action="edit.php?action=add.do" method="post" id="form" name="form">
<div class="box-con">
<table class="table-input">
        <tr>
                <td class="title">网点名称</td>
                <td><input name="name" type="text" class="text" size="30" value="<?= htmlspecialchars($name) ?>" > <?= show_field_error('name') ?></td>
        </tr>
        <tr>
                <td class="title">所属省份</td>
        <td>
                        <select id="province" name="province" onchange="getCities()" >
                        </select>
                        <select id="city" name="city" >
                        </select>
        </td>
        </tr>
        <tr>
                <td class="title"> </td>
                <td><input type="submit" name="" value="提交" class="btnA" /><input type="reset" name="" value="取消" class="btnB" /></td>
        </tr>
</table>
</div>
</form>
<?
} else if ($action == "edit.form") {
?>
<form action="edit.php?action=edit.do" method="post" id="form" name="form">
<input type="hidden" name="id" value="<?= $id ?>">
<div class="box-con">
<table class="table-input">
        <tr>
                <td class="title">网点名称</td>
                <td><input name="name" type="text" class="text" size="30" value="<?= htmlspecialchars($name) ?>" > <?= show_field_error('name') ?></td>
        </tr>
          <?php
                    if(!empty($price)){
                        list($province, $city) = explode(' ', $price);
                    ?>
                    <script type="text/javascript">
                        jQuery(function($) {
                           $('#province').append("<option selected='selected'><?=$province?></option>");
                           $('#city').append("<option selected='selected'><?=$city?></option>");
                        });
                    </script>
                   <?php
                    }
                   ?>
        <tr>
                <td class="title">所属省份</td>
        <td>
                       <select id="province" name="province" onchange="getCities()" >
                        </select>
                        <select id="city" name="city" >
                        </select>
        </td>
        </tr>
        <tr>
                <td class="title"> </td>
                <td><input type="submit" name="" value="修改" class="btnA" /></td>
        </tr>
</table>
</div>
</form>
<?php
}elseif($action=="members.form"){
$rsc = $database->queryArray("SELECT * FROM members_info WHERE region=".string_sql($name)." LIMIT ".(($page - 1) * $pagesize).", $pagesize");
?>
				<table class="table-blue">
					<thead>
						<tr>
							<th>编号</th>
							<th>姓名</th>
							<th>手机</th>
							<th>网点</th>
							<th>卡号</th>
							<th>操作</th>
						</tr>
					</thead>
					<tbody>
						<?
                                                for($i=0;$i<sizeof($rsc);$i++){
                                                $r = $rsc[$i];
                                                $j = 1;
                                                ?>
                                                <tr <?=iif($j%2==0, 'class="odd"')?>>
                                                        <td><?=$r['id']?></td>
                                                        <td><?=$r['name']?></td>
                                                        <td><?=$r['tel']?></td>
                                                        <td><?=$r['region']?></td>
                                                        <td><?=$r['card']?></td>
                                                        <td><a href="<?=_CONST_BACKEND_URL?>members/edit.php?action=view.form&id=<?= $r['id'] ?>">查看详细</a></td>
                                                </tr>
                                                <?
                                                $j++;
                                                }
                                                ?>
					</tbody>
				</table>
                               <div class="digg">
                                 <? pagination($page, $pagesize, $rs_count[0]["total"], "edit.php?action=members.form&page=%s%") ?>
                               </div>
<?php
}elseif($action=="members.view"){
$rsc = $database->queryArray("SELECT * FROM customers WHERE region=".string_sql($name)." LIMIT ".(($page - 1) * $pagesize).", $pagesize");
?>
				<table class="table-blue">
                        <thead>
                                <tr>
                                        <th>编号</th>
                                        <th>企业名称</th>
                                        <th>联系人</th>
                                        <th>联系电话</th>
                                        <th>网点</th>
                                        <th>企业注册号</th>
                                        <th>操作</th>
                                </tr>
                        </thead>
                        <tbody>
                                <?
                                for($i=0;$i<sizeof($rsc);$i++){
                                $r = $rsc[$i];
                                $j = 1;
                                ?>
                                <tr <?=iif($j%2==0, 'class="odd"')?>>
                                        <td><?=$r['id']?></td>
                                        <td><?=$r['company_name']?></td>
                                        <td><?=$r['contact_username']?></td>
                                        <td><?=$r['contact_tel']?></td>
                                          <td><?=$r['region']?></td>
                                        <td><?=$r['company_no']?></td>
                                        <td><a href="<?=_CONST_BACKEND_URL?>company/edit.php?action=view.form&id=<?= $r['id'] ?>">查看详细</a></td>
                                </tr>
                                <?
                                $j++;
                                }
                                ?>
                        </tbody>
                </table>
                               <div class="digg">
                                 <? pagination($page, $pagesize, $rs_count[0]["total"], "edit.php?action=members.view&page=%s%") ?>
                               </div>
<?php
}
?>
</div>
<?
include_once("../layout/_footer.php");
?>